package com.dlwx.firefighting.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;

import com.dlwx.firefighting.R;
import com.dlwx.firefighting.config.Constant;
import com.dlwx.firefighting.utils.DialogUtils;
import com.dlwx.firefighting.utils.SPUtils;
import com.dlwx.firefighting.utils.ToastUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.zhy.autolayout.AutoLinearLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 设置支付密码
 */
public class PayPswActivity extends BaseActivity {

    @BindView(R.id.title_left_one_btn)
    ImageView titleLeftOneBtn;
    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    @BindView(R.id.edit_oldpsw)
    EditText editOldpsw;
    @BindView(R.id.edit_newpsw)
    EditText editNewpsw;
    @BindView(R.id.edit_surepsw)
    EditText editSurepsw;
    @BindView(R.id.btn_commit_password)
    TextView btnCommitPassword;
    @BindView(R.id.oldpsw_al)
    AutoLinearLayout oldpswAl;
    @BindView(R.id.et_money)
    EditText etMoney;
    @BindView(R.id.switch1)
    Switch switch1;
    private String userId;
    private boolean isPay = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay_psw);
        ButterKnife.bind(this);
        userId = (String) SPUtils.get(this, Constant.USER_ID, "");
        isPay = (boolean) SPUtils.get(PayPswActivity.this, Constant.IS_PAY, false);
        boolean noPsw= (boolean) SPUtils.get(PayPswActivity.this, Constant.IS_NO_PSW, false);
        if(noPsw){
            String mm= (String) SPUtils.get(PayPswActivity.this,Constant.IS_NO_PSW_MONEY,"");
            etMoney.setText(mm);
            switch1.setChecked(true);
        }
        initView();
    }

    private void initView() {
        tvMiddleTitle.setText("支付设置");
        titleLeftOneBtn.setVisibility(View.VISIBLE);
        if (isPay) {
            oldpswAl.setVisibility(View.VISIBLE);
        } else {
            oldpswAl.setVisibility(View.GONE);
        }
        etMoney.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                switch1.setChecked(false);
            }
        });
        switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                   String money= etMoney.getText().toString().trim();
                    if(TextUtils.isEmpty(money)){
                        ToastUtils.toastForShort(PayPswActivity.this,"金额不能为空");
                        switch1.setChecked(false);
                    }else{
                        ToastUtils.toastForShort(PayPswActivity.this,"设置成功");
                        SPUtils.put(PayPswActivity.this,Constant.IS_NO_PSW,true);
                        SPUtils.put(PayPswActivity.this,Constant.IS_NO_PSW_MONEY,money);
                    }
                }else{
                    SPUtils.put(PayPswActivity.this,Constant.IS_NO_PSW,false);
                    SPUtils.put(PayPswActivity.this,Constant.IS_NO_PSW_MONEY,"");
                }
            }
        });
    }

    @OnClick({R.id.title_left_one_btn, R.id.btn_commit_password})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_one_btn:
                PayPswActivity.this.finish();
                break;
            case R.id.btn_commit_password:
                String oldPsw = editOldpsw.getText().toString();
                String newPsw = editNewpsw.getText().toString();
                String surePsw = editSurepsw.getText().toString();
                if (TextUtils.isEmpty(newPsw) || TextUtils.isEmpty(surePsw)) {
                    ToastUtils.toastForShort(PayPswActivity.this, "密码不能为空");
                    return;
                }
                if (!TextUtils.equals(newPsw, surePsw)) {
                    ToastUtils.toastForShort(PayPswActivity.this, "新密码不一致！请重新输入。");
                    return;
                }
                resetPsw(oldPsw, newPsw);
                break;
        }
    }

    private void resetPsw(String oldPsw, final String newPsw) {
        DialogUtils.showProgressDlg(this, "");
        OkGo.<String>post(Constant.PAY_PSW)
                .tag(this)
                .params("user_id", userId)
                .params("old_password", oldPsw)
                .params("new_password", newPsw)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
//                        DialogUtils.stopProgressDlg();
//                        DealResult dealResult = new DealResult();
//                        BaseBean bean = dealResult.dealBaseRes(PayPswActivity.this, response);
//                        if (bean != null) {
//                            if (bean.getStatus() == Constant.SUCESS) {
//                                ToastUtils.toastForShort(PayPswActivity.this, "密码设置成功！");
//                                SPUtils.put(PayPswActivity.this, Constant.IS_PAY, true);
//                                SPUtils.put(PayPswActivity.this, Constant.PAY_PASSWORD, newPsw);
//                                PayPswActivity.this.finish();
//                            } else {
//                                ToastUtils.toastForShort(PayPswActivity.this, bean.getMsg());
//                            }
//                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        DialogUtils.stopProgressDlg();
                        ToastUtils.toastForShort(PayPswActivity.this, getString(R.string.bad_net));
                        super.onError(response);
                    }
                });
    }
}
